package cn.signalpha.modules.com.controller.api;

import cn.hutool.core.date.DateUtil;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.entity.UserLevel;
import cn.signalpha.modules.com.entity.UserLevelLog;
import cn.signalpha.modules.com.service.UserLevelLogService;
import cn.signalpha.modules.com.service.UserLevelService;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.com.vo.UserLevelVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_USER_NAME + "/wap/user/level")
@Api(value = "用户等级", tags = "用户等级")
public class ApiUserLevelController extends BaseController {

	private final UserService userService;
	private final UserLevelService userLevelService;
	private final UserLevelLogService userLevelLogService;

	@GetMapping("/list")
	@ApiOperation(value = "等级列表", notes = "")
	public R<List<UserLevel>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
		return R.data(userLevelService.list(userLevelService.selectUserLevelWrapper(params)));
	}

	@GetMapping("/now")
	@ApiOperation(value = "当前用户等级信息", notes = "")
	public R<UserLevelVO> now() {
		Date endTime = new Date();
		Date beginTime = DateUtil.beginOfDay(endTime);
		User user = userService.getById(getUser().getUserId());
		UserLevel userLevel = userLevelService.getById(user.getLevel());
		UserLevelVO userLevelVO = new UserLevelVO();
		userLevelVO.setId(user.getId());
		userLevelVO.setAvatar(user.getAvatar());
		userLevelVO.setNickName(user.getNickName());
		userLevelVO.setLevel(user.getLevel());
		userLevelVO.setLevelImg(userLevel.getImg());
		userLevelVO.setExtNow(user.getExp());
		userLevelVO.setExtNext(0);
		userLevelVO.setExtDay(userLevelLogService.sumByParams(Kv.create().set("userId", user.getId()).set("createTimeBegin", beginTime).set("createTimeEnd", endTime)));
		UserLevel userLevelNext = userLevelService.getById(user.getLevel() + 1);
		if(ObjectUtil.isNotEmpty(userLevelNext)){
			userLevelVO.setExtNext(userLevelNext.getExpUp());
		}
		return R.data(userLevelVO);
	}

	@GetMapping("/log/page")
	@ApiOperation(value = "经验值记录", notes = "")
	public R<IPage<UserLevelLog>> logPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("userId", getUser().getUserId());
		QueryWrapper<UserLevelLog> queryWrapper = userLevelLogService.selectUserLevelLogWrapper(params);
		return R.data(userLevelLogService.page(Condition.getPage(query), queryWrapper));
	}

}

